//
//  SEGChangePhoneNumberViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/11.
//

#import "SEGChangePhoneNumberViewModel.h"
#import "SEGRequestManager.h"

@interface SEGChangePhoneNumberViewModel ()

/// SEGRequestManager
@property (nonatomic, strong) SEGRequestManager *changeManager;

@end

@implementation SEGChangePhoneNumberViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createRefreshImgSignal];
        [self seg_createCaptchasSignal];
        [self seg_createNextSignal];
    }
    return self;
}
- (void)seg_createRefreshImgSignal {
    @weakify(self);
    self.refreshImgSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        self.checkImgStr = @"";
        [self.changeManager requestSecurityRefreshCheckCodeWithFinished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                if ([self.changeManager.seg_segtelcaptchasbyregistermodel.imgCode length] > 0) {
                    NSData *_decodedImageData = [[NSData alloc] initWithBase64EncodedString:self.changeManager.seg_segtelcaptchasbyregistermodel.imgCode options:NSDataBase64DecodingIgnoreUnknownCharacters];
                    UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
                    [subscriber sendNext:_decodedImage];
                    [subscriber sendCompleted];
                } else {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
        
        return nil;
    }];
}
- (void)seg_createCaptchasSignal {
    @weakify(self);
    self.captchasSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        // 获取存储的手机号码
        NSString *tel = [SEGProfileManager sharedManager].profile.tel;
        if (self.checkImgStr.length == 0) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入图片验证码"}];
            [subscriber sendError:error];
            return nil;
        }
        [self.changeManager requestGetCaptchasByTel:tel imgCode:self.checkImgStr randomToken:self.changeManager.seg_segtelcaptchasbyregistermodel.randomToken Finished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                [subscriber sendNext:@""];
                [subscriber sendCompleted];
            } else {
                self.checkImgStr = @"";
                NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
        
        return nil;
    }];
}
- (void)seg_createNextSignal {
    @weakify(self);
    self.nextSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        // 获取存储的手机号码
        NSString *tel = [SEGLoginV2Manager segUserTelFromLocal];
        if (self.smsCodeStr.length == 0) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入手机号验证码"}];
            [subscriber sendError:error];
            return nil;
        }
        [self.changeManager requestValidateCaptchasByTel:tel smsCode:self.smsCodeStr password:nil Finished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                [subscriber sendNext:@""];
                [subscriber sendCompleted];
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
        
        return nil;
    }];
}

#pragma mark - Getter
- (SEGRequestManager *)changeManager {
    if (!_changeManager) _changeManager = [[SEGRequestManager alloc] init];
    return _changeManager;
}


@end
